﻿var controller = this.lookupController();
var status = controller.analysis().status;
var purStore = ref.datagrid2.getStore();
var ukCrossCharge = ref.UKCrossCharge.getValue();
var costCategoryValue = ref.costCategory.getValue();

if (status == 'launch' || status == 'resubmit') {
    var record = store.getUpdatedRecords()[0],
        flag = 'update';

    if (!record) {
        record = store.getNewRecords()[0];
        flag = 'add';
    }

    if (!record) {
        flag = 'reset';
    }

    if (record) {
        var grid3 = ref.datagrid3,
            store3 = grid3.getStore();

        if (flag == 'update') {
            if (record.modified) {

                if (record.modified.DescriptionOfSuppliesServices !== undefined) {
                    var count = 0;
                    for (var i = 0; i < store.getCount(); i++) {
                        var descriptionOfSuppliesServiceInfo = store.getAt(i).data.DescriptionOfSuppliesServices;
                        if (descriptionOfSuppliesServiceInfo == record.data.DescriptionOfSuppliesServices) {
                            count = count + 1;
                        }
                    }
                    if (count > 1) {
                        controller.showMsgTips('Description of Supplies Services already exist.');
                        record.set("DescriptionOfSuppliesServices", "");
                    } else {
                        var r = purStore.findRecord('DescriptionOfSuppliesServices', record.data.DescriptionOfSuppliesServices);
                        if (r) {
                            record.data.ProjectActivity = r.data.ProjectActivity;
                        }

                        //这里需要设置一个定时任务,这样可以延迟修改可以出发grid的datachanged事件.
                        var ratio = 12;

                        setTimeout(function () {

                            record.set('JASharingRatio', ratio);
                            record.set('LRSharingRatio', 100 - ratio);
                        }, 200);
                    }
                } else if (record.modified.JASharingRatio !== undefined || record.modified.LRSharingRatio !== undefined) {

                    function setCost(store, field, flag) {
                        var cost = 0;
                        for (var i = 0; i < store.getCount(); i++) {
                            var currentData = store.getAt(i);
                            var DescriptionOfSuppliesServices = currentData.data.DescriptionOfSuppliesServices;
                            //如果是c (Company Allocation) ,就取Company = JLRC.

                            if (flag == 'c') {
                                if (currentData.data.Company == 'CJLR') {
                                    cost = floatAdd(cost, currentData.get(field));
                                }
                            } else {
                                cost = floatAdd(cost, currentData.get(field));
                            }
                        }
                        if (cost) {
                            record.data.JACost = parseFloat(((cost * record.data.JASharingRatio) / 100).toFixed(2));
                            record.data.LRCost = cost - record.data.JACost;
                        } else {
                            record.data.JACost = 0;
                            record.data.LRCost = 0;
                        }
                    }

                    if (record.modified.JASharingRatio !== undefined) {
                        if (parseFloat(record.data.JASharingRatio) < 0 || parseFloat(record.data.JASharingRatio) > 100) {
                            record.set("JASharingRatio", 0);
                        } else {
                            record.data.LRSharingRatio = 100 - record.data.JASharingRatio;
                            if (ukCrossCharge == 'JLRCUK') {
                                setCost(purStore, 'TotalPrice');
                            } else {
                                setCost(store3, 'Cost', 'c');
                            }
                        }

                    } else if (record.modified.LRSharingRatio !== undefined) {
                        if (!Ext.isEmpty(record.data.LRSharingRatio)) {
                            if (parseFloat(record.data.LRSharingRatio) < 0 || parseFloat(record.data.LRSharingRatio) > 100) {
                                record.set("LRSharingRatio", 0);
                            } else {
                                record.data.JASharingRatio = 100 - record.data.LRSharingRatio;
                                if (ukCrossCharge == 'JLRCUK') {
                                    setCost(purStore, 'TotalPrice');
                                } else {
                                    setCost(store3, 'Cost', 'c');
                                }
                            }
                        }
                    }
                }
            }
        }
        record.commit();
    }
}

//加法运算
function floatAdd(arg1, arg2) {
    var r1, r2, m;
    try { r1 = arg1.toString().split(".")[1].length; } catch (e) { r1 = 0; }
    try { r2 = arg2.toString().split(".")[1].length; } catch (e) { r2 = 0; }
    m = Math.pow(10, Math.max(r1, r2));
    return (arg1 * m + arg2 * m) / m;
}
